<?php

declare(strict_types=1);

namespace app\model;

use think\facade\Session;
use think\Model;
use think\model\concern\SoftDelete;

/**
 * @mixin \think\Model
 */
class PostComment extends Model
{
  //

  protected $defaultSoftDelete = 0;

  use SoftDelete;


  public function post()
  {
    return $this->belongsTo(Post::class, 'post_id');
  }

  public function user()
  {
    return $this->belongsTo(User::class, 'user_id');
  }

  public function follows()
  {
    return $this->hasMany(PostCommentFollow::class, 'comment_id');
  }

  public function getAttitudeStatusAttr()
  {
    $user_id = Session::get('user_id');

    if (empty($user_id)) {
      return 0;
    }

    // TODO:加缓存
    $model_attitude = PostCommentAttitude::where('user_id', $user_id)
      ->where('comment_id', $this->getData('id'))
      ->find();
    if (empty($model_attitude)) {
      return 0;
    }

    return $model_attitude->attitude_status;
  }
}
